package com.lottery.system.dao.mapper;

import com.lottery.system.dao.dataobject.ActivityUserDO;
import com.lottery.system.service.enums.ActivityUserStatusEnum;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author: xiaoxie
 * create: 2024-09-09 19:09
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.dao.mapper
 * description: 活动人员数据层
 */
@Mapper
public interface ActivityUserMapper{
    /**
     * @description: 插入人员列表数据
     * @author: xiaoxie
     * @date: 2024/9/9 19:12
     * @param: [activityUserDOList]
     * @return: void
     **/

    @Insert("<script>" +
            " insert into activity_user (activity_id,user_id, user_name, status)" +
            " values <foreach collection = 'items' item='item' index='index' separator=','>" +
            " (#{item.activityId}, #{item.userId}, #{item.userName}, #{item.status})" +
            " </foreach>" +
            " </script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void bachInsert(@Param(value = "items") List<ActivityUserDO> activityUserDOList);

    /**
     * @description: 获取活动人员表信息
     * @author: xiaoxie
     * @date: 2024/9/10 17:23
     * @param: [activityId]
     * @return: java.util.List<com.lottery.system.dao.dataobject.ActivityUserDO>
     **/
    @Select("select id,activity_id,status,user_id,user_name " +
            " from activity_user " +
            " where activity_id = #{activityId}")
    List<ActivityUserDO> selectActivityUserById(@Param(value = "activityId") Long activityId);
    /**
     * @description: 通过人员列表查询人员信息
     * @author: xiaoxie
     * @date: 2024/9/15 23:19
     * @param: [ids]
     * @return: java.util.List<com.lottery.system.dao.dataobject.ActivityUserDO>
     **/
    @Select("<script>" +
            " SELECT user_id, status, user_name FROM activity_user " +
            " WHERE user_id IN " +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            "   #{item} " +
            " </foreach> " +
            "<if test='activityId != null'> " +
            "   AND activity_id = #{activityId} " +
            "</if> " +
            "</script>")
    List<ActivityUserDO> selectActivityUserListByIds(@Param("activityId")Long activityId,@Param(value = "items") List<Long> ids);

    /**
     * @description: 通过活动id和用户id列表更新活动用户表的状态
     * @author: xiaoxie
     * @date: 2024/9/12 21:46
     * @param: [activityId, userIds, status]
     * @return: void
     **/
    @Update("<script>" +
            " update activity_user set status = #{status}" +
            " where activity_id = #{activityId}" +
            " and user_id in" +
            " <foreach collection='userIds' item='userId' open='(' separator=',' close=')' >" +
            " #{userId}" +
            " </foreach>" +
            " </script>")
    void updateStatusByActivityIdAndUserIds(@Param("activityId") Long activityId
                                           ,@Param(value = "userIds") List<Long> userIds
                                           ,@Param(value = "status") String status);

    /**
     * @description: 通过id列表查询未被抽中人员的数量
     * @author: xiaoxie
     * @date: 2024/9/13 15:01
     * @param: [userIds]
     * @return: int
     **/
    @Select("<script>" +
            " select count(*) from activity_user" +
            " where user_id in " +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " and status = 'INIT' " +
            " and activity_id =#{activityId} " +
            " </script>")
    int selectCount(@Param(value = "items") List<Long> userIds
                   ,@Param(value = "activityId") Long activityId);
}
